<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { gamesApi } from '@/api/controller';
  import { ToastSuccess, ToastWarning, Toastloading, allNumber } from '@/utils/public';
  import serachGift from '@/components/serachGift/index.vue'
  const formLabelWidth = '160px'
  const dataForm = ref(),
  serachGiftRef = ref();
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
    list:[] as Array<any>,
    dialogType:''
  }),
  options = [
    {label:'关闭',value:0},
    {label:'开启',value:1},
  ]
  interface formQuert {
    gift_id:number
    nums:number
    pool_id:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    nums: [{ required: true, message: "请输入投放数量", trigger: "blur" }],
    gift_id: [{ required: true, message: "请选择礼物", trigger: "blur" }],
  }
  const handleSubmit = async()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      await gamesApi.addLuckExGiftForPool(form).then(()=>{
        ToastSuccess('投放礼物成功')
        loading.close()
        $emit('updateList')
        closeDialog()
      }).catch(()=>{
        loading.close()
      })
    })
  }
  const openDialog = async(query:any={})=> {
    const { id:pool_id='', pool_name='' } = query
    if(!pool_id) return ToastWarning('缺少 pool_id')
    action.title = `${pool_name}：投放礼物`
    action.dialogFormVisible = true
    form.pool_id = pool_id
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="40%"
      :title="action.title">
        <el-form
         ref="dataForm"
         :model="form"
         :rules="formRules"
         >
        <el-form-item :label-width="formLabelWidth" label="礼物" prop="gift_id">
          <serachGift
            version="0.0.1"
            :show_gift_type="[1,9]"
            v-model:gift_id="form.gift_id"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" prop="nums" label="投放数量">
          <el-input v-model="form.nums" placeholder="请输入投放数量" />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>